package com.future.ws.controller;

import com.future.ws.config.WebSocketServer;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;

/**
 * 消息控制器
 *
 * @author shiyong
 * 2020/4/27 17:11
 */
@Controller
@RequestMapping("/message")
public class MessageController {
    /**
     * 客户端消息显示页面
     *
     * @return java.lang.String
     * @author shiyong
     * 2020/9/18 9:12
    */
    @GetMapping("/page")
    public ModelAndView page() {

        return new ModelAndView("message");
    }

    /**
     * 服务端向客户端发送消息
     *
     * @param message 消息
     * @param authToken 客户端访问令牌（客户标识）
     * @return org.springframework.http.ResponseEntity<java.lang.String>
     * @author shiyong
     * 2020/9/18 9:04
    */
    @ResponseBody
    @PostMapping("/sendMessageToClient")
    public ResponseEntity<String> sendMessageToClient(String message, String authToken) throws IOException {
        WebSocketServer.sendInfo(message, authToken);

        return ResponseEntity.ok("消息发送成功！");
    }
}
